package com.qf.lyw1225.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.lyw1225.springboot.dao.UserMapper;
import com.qf.lyw1225.springboot.entity.User;
import com.qf.lyw1225.springboot.service.ILoginService;
import com.qf.lyw1225.springboot.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author :wsh
 * @date : 2024/12/25
 */
@Service
public class LoginServiceImpl implements ILoginService {
    @Resource
    private UserMapper userMapper;


    @Override
    public Result login(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        User user1 = userMapper.selectOne(queryWrapper);
        if(user1==null){
            return Result.error("用户名不存在,请去注册");
        }
        String password = user1.getPassword();
        if(!password.equals(user.getPassword())){
            return Result.error("密码错误");
        }
        return Result.success("登录成功");
    }

    @Override
    public Result register(User user) {
        if(user.getUsername().equals("")||user.getPassword().equals("")){
            return Result.error("用户名或密码不能为空");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        user = userMapper.selectOne(queryWrapper);
        if(user!=null){
            return Result.error("用户名已存在");
        }
        int insert = userMapper.insert(user);
        if(insert==0){
            return Result.error("注册失败");
        }
        return Result.success("注册成功");

    }

    @Override
    public Result selectById(Integer id) {
        User user = userMapper.selectById(id);
        if(user==null){
            return Result.error("用户不存在");
        }
        return Result.success(user);
    }
}
